package com.data.center.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.data.center.dictionary.AuthenticationType;
import com.data.center.dictionary.Availability;
import com.data.center.entity.Expert;
import com.data.center.entity.Person;
import com.data.center.exception.exception.BizException;
import com.data.center.mapper.ExpertMapper;
import com.data.center.mapper.PersonMapper;
import com.data.center.service.ExpertService;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class ExpertServiceImpl extends ServiceImpl<ExpertMapper,Expert> implements ExpertService {
    @Autowired
    private ExpertMapper expertMapper;
    @Resource
    private PersonMapper personMapper;
    @Override
    public List<Expert> findExpert(Person person) {
        String personId = person.getPersonId();
        if (StringUtil.isNotEmpty(personId)) {
            List<Expert> expertList = expertMapper.selectList(new LambdaQueryWrapper<Expert>().eq(Expert::getExpertId, personId));
            if (expertList.size() > 1) {
                throw new BizException("查询到多个结果");
            } else {
                return expertList;
            }
        }else {
            throw new BizException("人才ID为空");
        }
    }

    @Override
    public Expert addExpert(Person person) {
        String phone = person.getPhone();
        if (!StringUtil.isNotEmpty(phone)) {
            throw new BizException("违反E-M2原则，手机号为空");
        }
        List<Expert> expertList = findExpert(person);
        if (expertList.size() != 0) {
            return expertList.get(0);
        }
        String authentication = person.getAuthentication();
        if (!StringUtil.isNotEmpty(authentication)) {
            throw new BizException("认证状态为空");
        }
        if (!AuthenticationType.PUBLIC_SECURITY_TWO_ELEMENT.equals(authentication)
                || !AuthenticationType.PUBLIC_SECURITY_THREE_ELEMENT.equals(authentication)) {
            // TODO:使用公安部核验接口对身份信息进行核验
        }
        String personId = person.getPersonId();
        if (StringUtil.isNotEmpty(personId)) {
            Person personById = personMapper.getPersonById(personId);
            if (personById == null){
                throw new BizException("该id对应的人才不存在");
            }
            personById.setAuthentication(AuthenticationType.PUBLIC_SECURITY_THREE_ELEMENT.getKey());
            personById.setAuthenticator("NCIIC");
            personById.setAuthenticateTime(new Date());
            personMapper.updatePerson(personById);
        } else {
            throw new BizException("人才ID为空");
        }
        Expert expert = new Expert();
        expert.setExpertId(personId);
        expert.setCreationTime(new Date());
        expert.setCreator("");// TODO:接入人员管理系统后，需要修改
        expert.setAvailability(Availability.EXTRACTABLE.getKey());
        expertMapper.insert(expert);
        return expert;
    }
    @Override
    public Expert forceAddExpert(Person person){
        String personId = person.getPersonId();
        if (StringUtil.isNotEmpty(personId)) {
            Expert expert1 = expertMapper.selectById(personId);
            if (expert1 != null){
                throw new BizException("该人才ID已存在");
            }
            Person personById = personMapper.getPersonById(personId);
            personById.setAuthentication(AuthenticationType.BUSINESS_TWO_ELEMENT.getKey());
            personById.setAuthenticator("now_manager"); // TODO:接入人员管理系统后，需要修改
            personById.setAuthenticateTime(new Date());
            personMapper.updatePerson(personById);

        } else {
            throw new BizException("人才ID为空");
        }
        Expert expert = new Expert();
        expert.setExpertId(personId);
        expert.setCreationTime(new Date());
        expert.setCreator("");// TODO:接入人员管理系统后，需要修改
        expert.setAvailability(Availability.EXTRACTABLE.getKey());
        expertMapper.insert(expert);
        return expert;
    }

    @Override
    public List<Expert> searchByForeignKey(String id) {
        if (!StringUtil.isNotEmpty(id)){
            throw new BizException("id为空");
        }
        LambdaQueryWrapper<Expert> wrapperById = new LambdaQueryWrapper<>();
        wrapperById.eq(Expert::getExpertId,id);
        List<Expert> expertListById = expertMapper.selectList(wrapperById);
        LambdaQueryWrapper<Expert> wrapperByCreator = new LambdaQueryWrapper<>();
        wrapperByCreator.eq(Expert::getCreator,id);
        List<Expert> expertListByCreator = expertMapper.selectList(wrapperByCreator);

        ArrayList<Expert> expertArrayList = new ArrayList<>();
        expertArrayList.addAll(expertListById);
        expertArrayList.addAll(expertListByCreator);

        Set<Expert> set = new HashSet<>(expertArrayList);
        List<Expert> resultList = new ArrayList<>(set);
        return resultList;

    }

    @Override
    public int deleteByForeignKey(String id) {
        List<Expert> expertList = searchByForeignKey(id);
        if (expertList.size() == 0){
            return 0;
        }
        for (Expert item : expertList) {
            expertMapper.deleteById(item.getExpertId());
        }
        return 0;
    }

    @Override
    public int mergeByForeignKey(String id, String deletedId) {
        if (id.equals(deletedId)){
            return  0;
        }
        LambdaQueryWrapper<Expert> wrapperById = new LambdaQueryWrapper<>();
        wrapperById.eq(Expert::getExpertId,deletedId);
        List<Expert> expertListById = expertMapper.selectList(wrapperById);
        LambdaQueryWrapper<Expert> wrapperByCreator = new LambdaQueryWrapper<>();
        wrapperByCreator.eq(Expert::getCreator,deletedId);
        List<Expert> expertListByCreator = expertMapper.selectList(wrapperByCreator);
        if (expertListById.size() > 0) {
            Expert expert = expertMapper.selectById(id);
            if (expert != null){
                throw new BizException("需要融合的数据已存在");
            }
            Expert expert1 = new Expert();
            expert1.setExpertId(id);
            expertMapper.update(expert1,wrapperById);

        }
        if (expertListByCreator.size() > 0) {
            Expert expert = new Expert();
            expert.setCreator(id);
            expertMapper.update(expert,wrapperByCreator);
        }
        return 0;
    }


}
